package Main;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SpringLayout;

import entities.*;

public class TablePane extends JPanel  {
	private JPanel parent;
	private CardLayout cards;
	private FileReader fr;
	
	public TablePane(final JPanel parent, final CardLayout cards, FileReader fr){
		this.parent=parent;
		this.cards=cards;
		this.setLayout(null);
		this.fr = fr;
		createTables();
		this.setFocusable(true);
		JButton j = new JButton("Simulate");
		j.setBounds(200, 650, 750, 50);
		
		j.addActionListener(
				new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						cards.show(parent, 3+"");
					}
				}
		);
		this.add(j, BorderLayout.NORTH);
	}
	
	public void createTables(){
		String[][] ex =null;
		String[][] rd = new String[fr.getResourceDeclaration().size()][fr.getResourceDeclarationCol().length];
		String[][] p = new String[fr.getProcess().size()][fr.getProcessCol().length];
		String[][]sys = new String[1][fr.getSysConfigCol().length];
		int k=0;
		for(int i=0; i<fr.getProcess().size(); i++){
			 if(k==fr.getProcess().size())
				 break;
				
			p[i][0] = fr.getProcess().get(k).getProcessID()+"";
			p[i][1] = fr.getProcess().get(k).getCreationTime()+"";
			p[i][2] = fr.getProcess().get(k).getMemorySize()+"";
			p[i][3] = fr.getProcess().get(k).getPriority()+"";
			p[i][4] = fr.getProcess().get(k).getType();
			
			k++;
		}
		JTable exTable=null;
		String[] str = new String[fr.getExecSeqCol().length-1];
		CardLayout cl2;
		JPanel cards2 = new JPanel(cl2 = new CardLayout());
		
		int s=0;
		
		for(s=1; s<fr.getExecSeqCol().length; s++){
			str[s-1] = fr.getExecSeqCol()[s];
		}
	for(int r=0; r<fr.getExecSeq().size(); r++){
			ex = new String[fr.getExecSeq().get(0).size()][fr.getExecSeqCol().length];
			
			for(int i=0; i<fr.getExecSeq().get(r).size(); i++){
				
				ex[i][0] = fr.getExecSeq().get(r).get(i).getProcessID()+"";
				ex[i][1] = fr.getExecSeq().get(r).get(i).getSequence()+"";
				ex[i][2] = fr.getExecSeq().get(r).get(i).getExecute();
				ex[i][3] = fr.getExecSeq().get(r).get(i).getInstance()+"";
				ex[i][4] = fr.getExecSeq().get(r).get(i).getParameter();
				ex[i][5] = fr.getExecSeq().get(r).get(i).getDuration()+"";
				
				if(ex[i][3].equalsIgnoreCase(0+"")){
					ex[i][3] = null;
				}
				if(k==fr.getExecSeq().get(0).size()-1){
					ex[i][3] = null;
					ex[i][4] = null;
					ex[i][5] = null;
				}
				k++;
			 
			}
			if(exTable!=null)
			exTable.removeAll();
			exTable = new JTable(ex, str);
			cards2.add(new JScrollPane(new JPanel().add(exTable)), r+"");
		}
		cards2.setBounds(125, 375, 400, 250);
		this.add(cards2);
		
		k=0;
		for(int i=0; i<fr.getResourceDeclaration().size(); i++){
			if(k==fr.getResourceDeclaration().size())
				break;
			
			rd[i][0] = fr.getResourceDeclaration().get(k).getProcessID()+"";
			rd[i][1] = fr.getResourceDeclaration().get(k).getResource();
			rd[i][2] = fr.getResourceDeclaration().get(k).getMaximum()+"";
			k++;
		}
		k=0;
		 	
			sys[0][0] = fr.getSystemConfiguration().getNoProcess()+"";
			sys[0][1] = fr.getSystemConfiguration().getNoCPU()+"";
			sys[0][2] = fr.getSystemConfiguration().getNoResources()+"";
			sys[0][3] = fr.getSystemConfiguration().getNoGlobalData()+"";
			sys[0][4] = fr.getSystemConfiguration().getNoBuffers()+"";
			sys[0][5] = fr.getSystemConfiguration().getNoProcessTypes()+"";
			sys[0][6] = fr.getSystemConfiguration().getNoProcessPriorities()+"";
		 
		 
		String[] str1 = new String[fr.getResourceDeclarationCol().length-1];
		String[] str2 = new String[fr.getProcessCol().length-1];
		String[] str3= new String[fr.getSysConfigCol().length-1];
		
		for(s=1; s<fr.getResourceDeclarationCol().length; s++){
			str1[s-1] = fr.getResourceDeclarationCol()[s];
		}
		for(s=1; s<fr.getProcessCol().length; s++){
			str2[s-1]=fr.getProcessCol()[s];
		}
		for(s=1; s<fr.getSysConfigCol().length; s++){
			str3[s-1]=fr.getSysConfigCol()[s];
		}
		JTable rdTable = new JTable(rd, str1);
		JTable pTable = new JTable(p, str2);
		JTable sTable = new JTable(sys, str3);
		
		
	
		ButtonPanel buttonPanel = new ButtonPanel(fr, cards2, cl2, exTable);
		
		JScrollPane rdSP = new JScrollPane(rdTable);
		JScrollPane pSP = new JScrollPane(pTable);
		JScrollPane bSP = new JScrollPane(buttonPanel);
		JScrollPane sSP = new JScrollPane(sTable);
		rdSP.removeFocusListener(null);
		pSP.removeFocusListener(null);
		bSP.removeFocusListener(null);
		sSP.removeFocusListener(null);
		
		rdSP.setBounds(650, 375, 400, 250);
		pSP.setBounds(350, 70, 400, 180);
		bSP.setBounds(30, 375, 75, 250);
		sSP.setBounds(200, 295, 735, 45);
		
		this.add(rdSP);
		
		this.add(pSP);
		this.add(bSP);	
		this.add(sSP);
	}
}

